package com.lyn.base;

import android.app.Activity;
import android.content.Intent;

import androidx.annotation.NonNull;

import java.util.Stack;

/**
 * activity 帮助类
 *
 * @author longyn 2023/4/24
 * @version 1.0.0
 */
public class ActivityHelper {
    private static final Stack<Activity> activityStacks = new Stack<>();

    public static void push(Activity activity) {
        activityStacks.push(activity);
    }

    public static void remove(Activity activity) {
        activityStacks.remove(activity);
    }

    public static boolean hasActivity(Class<?> activityClass){
        if (activityClass == null)return false;
        for (Activity activity:activityStacks){
            if (activity.getClass().getName().equals(activityClass.getName())){
                return true;
            }
        }
        return false;
    }

    public static Activity getTopActivity() {
        if (activityStacks.isEmpty()) {
            return null;
        }
        return activityStacks.peek();
    }

    public static void jump(Class<?> activityClass) {
        jump(activityClass, true);
    }

    public static void backActivity(){
        if (activityStacks.size()>=2){
            activityStacks.pop();
            Activity peek = activityStacks.peek();
            jump(peek.getClass(),true);
        }
    }

    public static void jump(@NonNull Class<?> activityClass, boolean finish) {
        Activity topActivity = getTopActivity();
        if (topActivity == null) {
            return;
        }
        if (topActivity.getClass().getName().equals(activityClass.getName())) {
            return;
        }
        topActivity.startActivity(new Intent(topActivity, activityClass));
        if (finish) {
            topActivity.finish();
        }
    }
}
